import pymysql


# 数据库操作模块
# 封装一个数据库操作类 DB
# 读方法 read(sql) return 受影响行数, 结果集
# 写方法 write(sql) return 受影响行数
from jiekou.port_job.mall.utils.Getkeyword import Getkeyword


class DB:
    def __init__(self,database='mall', host='47.108.206.100', user='student', password='stu2022', port=3306, charset='utf8',
                  cursor_type = pymysql.cursors.DictCursor):
        # 初始化连接对象
        self.__conn = pymysql.connect(host=host, user=user, password=password, port=port, charset=charset,
                                      database=database)
        # 初始化游标对象
        # 游标类型: 元组, 字典
        self.__cursor = self.__conn.cursor()
        self.__cursor1 = self.__conn.cursor(cursor_type)

    def read(self, sql):
        try:
            # 执行sql语句
            rows = self.__cursor.execute(sql)
            # 获取结果集
            data = self.__cursor.fetchall()
        except Exception :
            print('sql语句有问题')
            return None, 'No Data'
        return rows, data

    def read1(self, sql):
        try:
            # 执行sql语句
            rows = self.__cursor1.execute(sql)
            # 获取结果集
            data = self.__cursor1.fetchall()
        except Exception :
            print('sql语句有问题')
            return None, 'No Data'
        return rows, data




    def __del__(self):
        # 对象销毁后执行的方法
        self.__cursor.close()
        self.__conn.close()


if __name__ == '__main__':

    sql = 'select delete_status, id from oms_cart_item where member_id = 1828 order by create_date desc'
    sql1 = 'select count(*) from oms_cart_item where member_id = 1828'
    row, data = DB().read1(sql1)
    print(data)
    # row, data = DB().read1(sql)
    # print(data)
    # data2 = data[0]
    # print(data2)
    #
    # print(Getkeyword.get_keyword(data2, 'id'))
